package com.zzw.message;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText editNumber;
    private EditText editTextContent;

    /** 发送与接收的广播 **/
    private static String SENT_SMS_ACTION = "SENT_SMS_ACTION";
    private static String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button sendButton = (Button)findViewById(R.id.sendButton);

        editNumber = (EditText)findViewById(R.id.editNumber);

        editTextContent = (EditText)findViewById(R.id.editTextContent);
        sendButton.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.sendButton:
                String number = editNumber.getText().toString().trim();
                String content = editTextContent.getText().toString().trim();

                if(TextUtils.isEmpty(number) || TextUtils.isEmpty(content)){
                    Toast.makeText(this,"号码或短信内容不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }

            // 打开android手机默认的短信管理器
                SmsManager smsManager = SmsManager.getDefault();
                // 当短信内容过长时将信息拆分成几条短信发送
                ArrayList<String> contents = smsManager.divideMessage(content);
                for(String str : contents) {
                    smsManager.sendTextMessage(number, null, str, null, null);
                }
                Toast.makeText(this,"发送成功",Toast.LENGTH_SHORT).show();
                break;
        }
    }
        /*private static BroadcastReceiver sendMessage = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 判断短信是否发送成功
            switch (getResultCode()){
                case Activity.RESULT_OK:
                    Toast.makeText(context,"短信发送成功",Toast.LENGTH_SHORT);
                    break;
                default:
                    Toast.makeText(context,"短信发送失败！",Toast.LENGTH_SHORT);
                    break;
            }
        }
    };

    private static BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 判断短信接收情况
            Toast.makeText(context,"对方接收成功",Toast.LENGTH_SHORT);
        }
    };*/
}
